package me.aip.mov.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import me.aip.mov.entity.ChatFriends;
import me.aip.mov.entity.UserInfo;
import me.aip.mov.mapper.ChatFriendsMapper;
import me.aip.mov.mapper.UserInfoMapper;
import me.aip.mov.service.IUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.aip.mov.util.QRCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aip9105
 * @since 2022-02-12
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {

    @Autowired
    private ChatFriendsMapper chatFriendsMapper;

    @Override
    public UserInfo getPwdAndSaltById(String userId) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.select(UserInfo::getUserId, UserInfo::getPassword,UserInfo::getSalt);
        queryWrapper.eq(UserInfo::getUserId,userId);
        return this.baseMapper.selectOne(queryWrapper);
    }


    @Override
    public UserInfo getUserByUsername(String username) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(UserInfo::getUserName,username);
        return this.baseMapper.selectOne(queryWrapper);
    }



    @Override
    public UserInfo getUserByPhone(String phone) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.select(UserInfo::getUserId, UserInfo::getPassword);
        queryWrapper.eq(UserInfo::getPhone,phone);
        return this.baseMapper.selectOne(queryWrapper);
    }


    @Override
    public List<UserInfo> getFriendList(String userId) {
        LambdaQueryWrapper<ChatFriends> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(ChatFriends::getFuserid);
        queryWrapper.eq(ChatFriends::getUserid,userId);
        List<ChatFriends> chatFriends = chatFriendsMapper.selectList(queryWrapper);
        List<String> friendIds = chatFriends.stream().map(ChatFriends::getFuserid).collect(Collectors.toList());
        return this.baseMapper.selectBatchIds(friendIds);
    }

    @Override
    public UserInfo findKf() {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getIskf,1);
        queryWrapper.last("limit 1");
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public String getPromoCode(String userId) {
        UserInfo userInfo = this.baseMapper.selectById(userId);

        String text = userInfo.getPromoCode(); //这里设置自定义网站url或文字
        String logoPath = userInfo.getAvatar(); //二维码图片
        try {
            return QRCodeUtil.encodeBase64(text, logoPath, true,true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
